package com.himiotech.core.project.web.system;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.himiotech.core.framework.helper.anno.Log;
import com.himiotech.core.framework.helper.constant.UserConstants;
import com.himiotech.core.framework.helper.enums.BusinessType;
import com.himiotech.core.framework.helper.utils.ExcelUtil;
import com.himiotech.core.project.model.entity.Post;
import com.himiotech.core.project.model.metadata.PostMetadata;
import com.himiotech.core.project.service.IPostService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Controller
@RequestMapping("/system/post")
public class PostController extends HimioBaseController {

    @Autowired
    private IPostService postService;

    @RequiresPermissions("system:post:view")
    @GetMapping()
    public String post() {
        return "system/post";
    }

    /**
     * 查询岗位信息表列表
     *
     * @param page 分页信息
     * @param post 查询条件
     * @return 表格分页数据
     */
    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public HimioResult list(Page<Post> page, Post post) {
        handleOrderBy(request, page);
        page = postService.selectPostListPage(page, post);
        return writeJsonLayuiTable(page);
    }

    /**
     * 导出岗位信息表列表数据
     *
     * @param post 查询条件
     * @return 导出数据
     */
    @Log(title = "岗位信息表 - 导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:post:export")
    @PostMapping("/export")
    @ResponseBody
    public HimioResult export(Post post) {
        QueryWrapper<Post> query = Wrappers.query();
        if (post != null) {
            query.like(ObjectUtil.isNotEmpty(post.getPostCode()), PostMetadata.POST_CODE.getColName(), post.getPostCode());
            query.like(ObjectUtil.isNotEmpty(post.getPostName()), PostMetadata.POST_NAME.getColName(), post.getPostName());
            query.eq(ObjectUtil.isNotEmpty(post.getStatus()), PostMetadata.STATUS.getColName(), post.getStatus());

        }
        List<Post> list = postService.list(query);
        ExcelUtil<Post> util = new ExcelUtil<>(Post.class);
        return util.exportExcel(list, "岗位信息表");
    }

    @RequiresPermissions("system:post:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public HimioResult remove(String ids) {
        try {
            return writeJson(postService.deletePostByIds(ids));
        } catch (Exception e) {
            return writeJsonErr(e.getMessage());
        }
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("system:post:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public HimioResult addSave(@Validated Post post) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return writeJsonErr("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return writeJsonErr("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return writeJson(postService.save(post));
    }


    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:post:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public HimioResult editSave(@Validated Post post) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
            return writeJsonErr("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
            return writeJsonErr("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return writeJson(postService.updateById(post));
    }

    /**
     * 校验岗位名称
     */
    @PostMapping("/checkPostNameUnique")
    @ResponseBody
    public String checkPostNameUnique(Post post) {
        return postService.checkPostNameUnique(post);
    }

    /**
     * 校验岗位编码
     */
    @PostMapping("/checkPostCodeUnique")
    @ResponseBody
    public String checkPostCodeUnique(Post post) {
        return postService.checkPostCodeUnique(post);
    }
}
